// ListViewDialog.cpp

#include "StdAfx.h"

#include "ListViewDialog.h"
#include "Common/Registry/RegistryUtils.h"

#ifdef LANG
#include "Common/Lang/LangUtils.h"

using namespace Common;

static Lang::CIDLangPair kIDLangPairs[] =
{
  { IDOK, 0x02000702 },
  { IDCANCEL, 0x02000710 }
};
#endif

LRESULT CListViewDialog::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
  #ifdef LANG
  Lang::LangSetDlgItemsText(HWND(*this), kIDLangPairs, sizeof(kIDLangPairs) / sizeof(kIDLangPairs[0]));
  #endif
  _listView.Attach(GetDlgItem(IDC_LISTVIEW_LIST));

  if (Registry::ReadSingleClick())
    _listView.SetExtendedListViewStyle(LVS_EX_ONECLICKACTIVATE | LVS_EX_TRACKSELECT);

  SetWindowText(Title);

  LVCOLUMN columnInfo;
  columnInfo.mask = LVCF_FMT | LVCF_WIDTH | LVCF_SUBITEM;
  columnInfo.fmt = LVCFMT_LEFT;
  columnInfo.iSubItem = 0;
  columnInfo.cx = 200;

  _listView.InsertColumn(0, &columnInfo);

  int itemIndex(0);
  for (size_t i = 0; i < Strings.size(); i++)
  {
    itemIndex = _listView.InsertItem(i, Strings[i]);
	_listView.SetItemData(itemIndex, i);
  }

  if (Strings.size() > 0)
    _listView.SetItemState(0, LVIS_FOCUSED | LVIS_SELECTED, LVIS_FOCUSED | LVIS_SELECTED);

  _listView.SetColumnWidth(0, LVSCW_AUTOSIZE);
  StringsWereChanged = false;

  CenterWindow(GetParent());
  return TRUE;
}

LRESULT CListViewDialog::OnSize(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/)

{
  int xSize = LOWORD(lParam);
	int ySize = HIWORD(lParam);
	int mx, my;
  RECT rect;
    rect.left = 0;
    rect.top = 0;
    rect.right = 8;
    rect.bottom = 8;
    MapDialogRect(&rect);
     
    mx = rect.right - rect.left;
    my = rect.bottom - rect.top;
  int bx1, bx2, by;
  
  CWindow wndCancel = GetDlgItem(IDCANCEL);
  wndCancel.GetWindowRect(&rect);
  bx1 = rect.right - rect.left;
  by = rect.bottom - rect.top;
  CWindow wndOK = GetDlgItem(IDOK);
  wndOK.GetWindowRect(&rect);
  bx2 = rect.right - rect.left;
  int y = ySize - my - by;
  int x = xSize - mx - bx1;
  
  InvalidateRect(NULL);

  wndCancel.MoveWindow(x, y, bx1, by);
  wndOK.MoveWindow(x - mx - bx2, y, bx2, by);
  _listView.MoveWindow(mx, my, xSize - mx * 2, y - my * 2);
  return false;
}


LRESULT CListViewDialog::OnNotify(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/)
{
  LPNMHDR header =	(LPNMHDR)lParam;
  if (header->hwndFrom != _listView)
    return false;
   BOOL result;
  switch(header->code)
  {
    case LVN_ITEMACTIVATE:
      OnCloseCmd(0, IDOK, m_hWnd, result);
      return TRUE;
      
      break;
    case NM_DBLCLK:
    case NM_RETURN: // probabably it's unused
        OnCloseCmd(0, IDOK, m_hWnd, result);
        return TRUE;      
      break;

    case LVN_KEYDOWN:
    {
      LPNMLVKEYDOWN keyDownInfo = LPNMLVKEYDOWN(header);
      switch(keyDownInfo->wVKey)
      {
        case VK_DELETE:
        {
          if (!DeleteIsAllowed)
            return FALSE;
          for (;;)
          {
			  int index = _listView.GetNextItem(-1, TVGN_NEXTSELECTED);//  GetNextSelectedItem(-1);
            if (index < 0)
              break;
            StringsWereChanged = true;
            _listView.DeleteItem(index);
			Strings.erase(Strings.begin() + index);
          }
          int focusedIndex = _listView.GetNextItem(-1, LVNI_FOCUSED);
          if (focusedIndex >= 0)
            _listView.SetItemState(focusedIndex, LVIS_FOCUSED | LVIS_SELECTED, LVIS_FOCUSED | LVIS_SELECTED);
          _listView.SetColumnWidth(0, LVSCW_AUTOSIZE);
          return TRUE;
        }
        case 'A':
        {
          // probably that code is unused ?
          /*
          bool ctrl = (::GetKeyState(VK_CONTROL) & 0x8000) != 0;
          if (ctrl)
          {
            int numItems = _listView.GetItemCount();
            for (int i = 0; i < numItems; i++)
              _listView.SetItemState(i, LVIS_SELECTED, LVIS_SELECTED);
            return true;
          }
          */
        }
      }
    }
  }
  return FALSE;
}


